文章目录
六大部件的关系容器——结构与分类sequence 容器使用容器array——代码案例使用容器vector——代码案例使用容器list——代码案例使用容器forward list——代码案例使用容器deque——代码案例
关联式容器使用容器multiset——代码案例使用容器multimap——代码案例使用容器set——代码案例使用容器map——代码案例
unordered 容器使用容器unordered_multiset——代码案例使用容器unordered_multimap——代码案例
六大部件的关系
![在这里插入图片描述](https://img-blog.csdnimg.cn/db6499b35ce04bc2ad97080fc3bc3e20.png)
容器——结构与分类
![在这里插入图片描述](https://img-blog.csdnimg.cn/9996d9723bf3408187e14eeb230a393e.png)
set与multiset的区别:set中的key值(key就是value)不可以重复,而multiset可以重复。 实现底层是红黑树,保持高度平衡的二叉树。自动调整
sequence 容器
使用容器array——代码案例
#include
#include
#include
#include //qsort, bsearch, NULL
namespace jj01
{
void test_array()
{
cout
long target=0;
cout
cout
snprintf(buf, 10, "%d", rand());
c.push_back(string(buf)); //尾部插入,双倍增长
}
catch(exception& p) {
cout
timeStart = clock();
sort(c.begin(), c.end());
cout
try {
snprintf(buf, 10, "%d", rand());
c.push_back(string(buf));
}
catch(exception& p) {
cout
try {
snprintf(buf, 10, "%d", rand());
c.push_front(string(buf));
}
catch(exception& p) {
cout
try {
snprintf(buf, 10, "%d", rand());
c.push_back(string(buf));
}
catch(exception& p) {
cout
try {
snprintf(buf, 10, "%d", rand());
c.insert(string(buf));
}
catch(exception& p) {
cout
timeStart = clock();
auto pItem = c.find(target); //比 std::find(...) 快很多
cout
try {
snprintf(buf, 10, "%d", rand());
//multimap 不可使用 [] 做 insertion
c.insert(pair(i,buf)); //key为0~1000000,value为随机数
}
catch(exception& p) {
cout
try {
snprintf(buf, 10, "%d", rand());
c.insert(string(buf));
}
catch(exception& p) {
cout
timeStart = clock();
auto pItem = c.find(target); //比 std::find(...) 快很多
cout
try {
snprintf(buf, 10, "%d", rand());
c[i] = string(buf);
}
catch(exception& p) {
cout
try {
snprintf(buf, 10, "%d", rand());
c.insert(string(buf));
}
catch(exception& p) {
cout
timeStart = clock();
auto pItem = c.find(target); //比 std::find(...) 快很多
cout
try {
snprintf(buf, 10, "%d", rand());
//multimap 不可使用 [] 進行 insertion
c.insert(pair(i,buf));
}
catch(exception& p) {
cout |